package com.zu.commu.auto1_0;

import com.zu.commu.auto1_0.protocol.DataProtocol;
import com.zu.commu.auto1_0.protocol.OrderProtocol;
import com.zu.commu.interf.Action;
import com.zu.commu.interf.Command;
import com.zu.commu.interf.Data;
import com.zu.commu.interf.Order;
import com.zu.commu.interf.meter.Request;
import org.apache.log4j.Logger;

public class RequestImp implements Request {
	private static Logger log = Logger
			.getLogger("com.automic.commu.auto1_0.RequestImp");
	private byte[] remoteData;
	private Data[] centerData;
	private Order order;
	private String error;

	public Action analyseData(byte[] data) {
		DataType1_0 type = null;
		try {
			type = new DataProtocol().decideDataType(data);
			// 如果上报数据是<dataType>205</dataType> 则是主动上报数据
			if (type.equals(DataType1_0.REPORTDATA)) {
				this.order = Order.CALL;
				this.centerData = new DataProtocol().analyseReportData(data);
				return Action.SENDCENTER;
			}
			if (type.equals(DataType1_0.SETCLOCK)) {//新协议占用
				this.order = Order.CALL;
				//this.centerData = new DataProtocol().analyseSetClockData(data);
				this.centerData = new DataProtocol().analyseReportData2018(data);
				return Action.SENDCENTER;
			}
			if (type.equals(DataType1_0.READCLOCK)) {
				this.order = Order.READCLOCK;
				this.centerData = new DataProtocol().analyseReadClockData(data);
				return Action.SENDCENTER;
			}
			if (type.equals(DataType1_0.PUMP)) {
				this.order = Order.PUMP;
				this.centerData = new DataProtocol().analyseSetPumpData(data);
				return Action.SENDCENTER;
			}
			if (type.equals(DataType1_0.INITVALUE)) {
				this.order = Order.INITVALUE;
				this.centerData = new DataProtocol()
						.analyseSetInitValueData(data);
				return Action.SENDCENTER;
			}
			if (type.equals(DataType1_0.CHARGE)) {
				this.order = Order.CHARGE;
				DataProtocol dp = new DataProtocol();
				this.centerData = dp.analyseChargeData(data);

				if ((!dp.getChargeSucc()) && (dp.getChargeTimes() != 1)
						&& (dp.getChargeTimes() != 0)
						&& (this.centerData != null)
						&& (this.centerData.length > 0)) {
					this.remoteData = new OrderProtocol()
							.reCreateChargeOrderData(this.centerData[0]
									.getMeterNo(), ""
									+ this.centerData[0].getChargeAmount()
											.intValue(), "1");
					this.centerData = null;
					return Action.SENDREMOTE;
				}

				if ((!dp.getChargeSucc()) && (dp.getChargeTimes() == 1)) {
					this.remoteData = new OrderProtocol()
							.reCreateChargeOrderData(this.centerData[0]
									.getMeterNo(), ""
									+ this.centerData[0].getChargeAmount()
											.intValue(), "0");
					this.centerData = null;
					return Action.SENDREMOTE;
				}
				return Action.SENDCENTER;
			}
			if (type.equals(DataType1_0.CHARGETIMES)) {
				this.order = Order.CHARGETIMES;
				this.centerData = new DataProtocol()
						.analyseSetChargeTimesData(data);
				return Action.SENDCENTER;
			}
			if (type.equals(DataType1_0.READCHARGE)) {
				this.order = Order.READCHARGE;
				this.centerData = new DataProtocol()
						.analyseReadChargeData(data);
				return Action.SENDCENTER;
			}
			if (type.equals(DataType1_0.REPORT24DATA)) {
				this.order = Order.REPORT24DATA;
				this.centerData = new DataProtocol().analyseCall24Data(data);
				return Action.SENDCENTER;
			}
			if (type.equals(DataType1_0.REPORT20DATEDATA)) {
				this.order = Order.REPORT20DATEDATA;
				this.centerData = new DataProtocol()
						.analyseCall20DateData(data);
				return Action.SENDCENTER;
			}
			if (type.equals(DataType1_0.CLEANALL)) {
				this.order = Order.CLEANALL;
				this.centerData = new DataProtocol().analyseCleanData(data);
				return Action.SENDCENTER;
			}
			if (type.equals(DataType1_0.WATERWARNING)) {
				this.order = Order.WATERWARNING;
				this.centerData = new DataProtocol()
						.analyseWaterLineWarningData(data);
				return Action.SENDCENTER;
			}
			if (type.equals(DataType1_0.WATERSITEWARNING)) {
				this.order = Order.WATERSITEWARNING;
				this.centerData = new DataProtocol()
						.analyseWaterSiteWarningData(data);
				return Action.SENDCENTER;
			}
			if (type.equals(DataType1_0.WATERLEVEL)) {
				this.order = Order.WATERLEVEL;
				this.centerData = new DataProtocol()
						.analyseWaterLevelData(data);
				return Action.SENDCENTER;
			}
			if (type.equals(DataType1_0.REPORTTIME)) {
				this.order = Order.SETREPORTTIME_2_0;
				this.centerData = new DataProtocol()
						.analyseReportTimeData(data);
				return Action.SENDCENTER;
			}

			this.order = null;
			this.error = ("对应的测控器不支持命令类型:" + type.toInt());
			return Action.ERROR;
		} catch (Exception e) {
			log.error(e.getMessage(), e);
			this.error = e.getMessage();
		}
		return Action.ERROR;
	}

	public Action analyseOrder(Order order, Command command) {
		String meterNo = command.getMeterNo();
		try {
			if ((Order.CALL.equals(order))
					|| (Order.READDATA_2_0.equals(order))) {
				this.remoteData = new OrderProtocol()
						.createCallOrderData(meterNo);
				return Action.SENDREMOTE;
			}

			if ((Order.SETCLOCK.equals(order))
					|| (Order.SETCLOCK_2_0.equals(order))) {
				this.remoteData = new OrderProtocol().createSetClockOrderData(
						meterNo, command.getPara1());
				return Action.SENDREMOTE;
			}

			if ((Order.READCLOCK.equals(order))
					|| (Order.READCLOCK_2_0.equals(order))) {
				this.remoteData = new OrderProtocol()
						.createReadClockOrderData(meterNo);
				return Action.SENDREMOTE;
			}

			if (Order.PUMP.equals(order)) {
				this.remoteData = new OrderProtocol().createPumpOrderData(
						meterNo, command.getPara1());
				return Action.SENDREMOTE;
			}

			if (Order.SETOPENPUMP_2_0.equals(order)) {
				this.remoteData = new OrderProtocol().createPumpOrderData(
						meterNo, "start");
				return Action.SENDREMOTE;
			}

			if (Order.SETCLOSEPUMP_2_0.equals(order)) {
				this.remoteData = new OrderProtocol().createPumpOrderData(
						meterNo, "stop");
				return Action.SENDREMOTE;
			}

			if ((Order.INITVALUE.equals(order))
					|| (Order.SETINITVALUE_2_0.equals(order))) {
				this.remoteData = new OrderProtocol().createInitValueOrderData(
						meterNo, command.getPara1());
				return Action.SENDREMOTE;
			}

			if ((Order.CHARGE.equals(order))
					|| (Order.SETCHARGE_2_0.equals(order))) {
				this.remoteData = new OrderProtocol().createChargeOrderData(
						meterNo, command.getPara1(), command.getPara2());
				return Action.SENDREMOTE;
			}

			if (Order.CHARGETIMES.equals(order)) {
				this.remoteData = new OrderProtocol()
						.createChargeTimesOrderData(meterNo, command.getPara1());
				return Action.SENDREMOTE;
			}

			if ((Order.READCHARGE.equals(order))
					|| (Order.READCHARGE_2_0.equals(order))) {
				this.remoteData = new OrderProtocol()
						.createReadChargeOrderData(meterNo);
				return Action.SENDREMOTE;
			}

			if (Order.REPORT24DATA.equals(order)) {
				this.remoteData = new OrderProtocol()
						.createCall24DataOrderData(meterNo);
				return Action.SENDREMOTE;
			}

			if (Order.REPORT20DATEDATA.equals(order)) {
				this.remoteData = new OrderProtocol()
						.createCall20DateDataOrderData(meterNo,
								command.getPara1());
				return Action.SENDREMOTE;
			}

			if ((Order.CLEANALL.equals(order))
					|| (Order.SETCLEAN_2_0.equals(order))) {
				this.remoteData = new OrderProtocol()
						.createCleanOrderData(meterNo);
				return Action.SENDREMOTE;
			}

			if (Order.WATERSITEWARNING.equals(order)) {
				this.remoteData = new OrderProtocol()
						.createWaterSiteWarningOrderData(meterNo,
								command.getPara1());
				return Action.SENDREMOTE;
			}

			if ((Order.WATERWARNING.equals(order))
					|| (Order.SETWATERREMAINWARNING_2_0.equals(order))) {
				this.remoteData = new OrderProtocol()
						.createWaterWarningOrderData(meterNo,
								command.getPara1());
				return Action.SENDREMOTE;
			}

			if ((Order.WATERLEVEL.equals(order))
					|| (Order.SETWATERLEVELOPTION_2_0.equals(order))) {
				this.remoteData = new OrderProtocol()
						.createWaterLevelOrderData(meterNo, command.getPara1());
				return Action.SENDREMOTE;
			}

			if (Order.READGPRSOPTION.equals(order)) {
				this.remoteData = null;
				return Action.SENDREMOTE;
			}

			if (Order.SETGPRSOPTION.equals(order)) {
				this.remoteData = null;
				return Action.SENDREMOTE;
			}

			if (Order.SETREPORTTIME_2_0.equals(order)) {
				this.remoteData = new OrderProtocol()
						.createReportTimeOrderData(meterNo, command.getPara1());
				return Action.SENDREMOTE;
			}

			this.error = ("对应的测控器不支持命令类型:" + order.toString());
			return Action.ERROR;
		} catch (Exception e) {
			log.error(e.getMessage(), e);
			this.error = e.getMessage();
		}
		return Action.ERROR;
	}

	public Order dataFromwhatOrder() {
		return this.order;
	}

	public byte[] getRemoteData() {
		return this.remoteData;
	}

	public Data[] getCenterData() {
		if (this.centerData != null) {
			for (int i = 0; i < this.centerData.length; i++) {
				if (this.centerData[i].getDataType() == null) {
					this.centerData[i].setDataType(this.order.toString());
				}
			}
		}
		return this.centerData;
	}

	public String getError() {
		return this.error;
	}
}